package com.example.cupid.controller;

import com.example.cupid.pojo.Tags;
import com.example.cupid.pojo.User;
import com.example.cupid.service.Impl.UserServiceImpl;
import com.example.cupid.service.Impl.WechatServiceImpl;
import com.example.cupid.service.TagService;
import com.example.cupid.util.DateUtil;
import com.example.cupid.util.MailUtil;
import com.example.cupid.util.Verification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private TagService tagService;

    @GetMapping
    public String login() {
        return "login_reg";
    }

    @GetMapping("/connectAdmin")
    public String connectUs(){ return "connectUs"; }

    @GetMapping("/index")
    public String toIndex() {
        return "index";
    }

    @GetMapping("/login")
    @ResponseBody
    public boolean login(@RequestParam String email, @RequestParam String password, HttpServletRequest request) {
        User loginUser = userService.findUser(email, password);
        if (null != loginUser) {
            HttpSession session = request.getSession();
            session.setAttribute("loginUser", loginUser);
            List<Tags> tags = tagService.getTags();
            session.setAttribute("tags",tags);
            return true;
        } else {
            return false;
        }
    }
    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("loginUser");
        return "redirect:/user/index";
    }
//    用户注册
    @GetMapping("/reg")
    @ResponseBody
    public Map<String, Object> register(@RequestParam String email,
                                        @RequestParam String password,
                                        @RequestParam String confirm,
                                        @RequestParam String verification,
                                        HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        String msg = "";
        boolean status = true;
        User user = userService.findUserByEmail(email);
        if(user!=null){
            msg+="【邮箱已被注册】";
            status=false;
        }
        if(!password.equals(confirm)){
            msg+="【密码不一致】";
            status = false;
        }
        HttpSession session = request.getSession();
        String verificationExist = (String)session.getAttribute(email + "Verification");
        if (verificationExist!=null && verificationExist.equals(verification)){
            Date emailTime =(Date) session.getAttribute(email + "Time");
            if(DateUtil.getDateDifference(new Date(),emailTime)>300000){
                session.removeAttribute(email+"Time");
                msg+="【验证码已过期】";
                status = false;
            }
        }
        if (verificationExist!=null && !verificationExist.equals(verification)){
            msg+="【验证码错误】";
            status = false;
        }
        if (verificationExist==null){
            msg+="【请获取验证码】";
            status = false;
        }
        map.put("msg",msg);
        map.put("status",status);
        java.util.Date utilDate=new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(utilDate);
        java.sql.Date sqlDate=new java.sql.Date(utilDate.getTime());
        if(status){
            userService.addUser(email,password,email,sqlDate,0);
        }
        return map;

    }
    //    发送验证码
    @ResponseBody
    @GetMapping("/authenticate")
    public Map<String,Object> authenticate(@RequestParam String email, HttpServletRequest request){
        Map<String,Object> result = new HashMap<>();
//        检查是否在5分钟内为该邮箱发送过验证码
        HttpSession session = request.getSession();
        Object  verificationExist= session.getAttribute(email + "Verification");
//        发送过
        if(verificationExist!=null){
            Date verificationTime =(Date) session.getAttribute(email + "Time");
            long dateDifference = DateUtil.getDateDifference(new Date(), verificationTime);
//            验证码未失效
            if(dateDifference<300000){
                result.put("status","验证码未失效，请不要重复点击");
                return result;
            }
        }
//        发送邮件
        String title = "高校丘比特系统通知";
        String verification = Verification.getVerification();
        String content = "【高校丘比特】验证码"+verification+"，用于认证用户注册，请在5分钟内完成验证，如非本人操作，请忽略本条邮件。";
        MailUtil mailUtil = new MailUtil();
        try{
            boolean b = mailUtil.sendMail(email, title, content);
            if(b){
//            验证码发送成功，将发送时间存放到session中
                session.setAttribute(email+ "Time",new Date());
//            验证码发送成功，将验证码存放到session中
                session.setAttribute(email+"Verification",verification);
                result.put("status","验证码发送成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.put("status","验证码发送失败");
        }
        return result;
    }
}
